package org.cos.verification.verifier;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

import org.cos.verification.exception.VerifierException;
import org.shellex.javame.utility.Screen;

public class RuleEditorScreen extends Screen {
	public Form screen; 
	
	private Command backCmd;
	private Command saveRuleCmd;
	private MIDlet midlet;
	
	static RuleEditorScreen instance;
	TextField ruleName;
	TextField ruleDescrib;
	TextField ruleContent;
	
	public static RuleEditorScreen getInstance()
	{
		if(instance == null)
			instance = new RuleEditorScreen();
		return instance;
	}
	
	public void crackCommand(MIDlet midlet, Command cmd, Displayable screen) {
		if (cmd == saveRuleCmd) {
			saveModification();
		} else if (cmd == backCmd){
			RuleViewerScreen.getInstance().display(midlet);
		}
	}

	public void saveModification(){
		Rule rule = new Rule(ruleName.getString(), ruleDescrib.getString(), ruleContent.getString());
		try {
			RuleManager.getInstance().setRule(rule);
		} catch (VerifierException e) {
			Alert alert = new Alert("Save Rule...");
			alert.setType(AlertType.INFO);
			alert.setString(String.valueOf("Save Fail"));
			Display.getDisplay(midlet).setCurrent(alert);
			e.printStackTrace();
		}
		Alert alert = new Alert("Save Rule...");
		alert.setType(AlertType.INFO);
		alert.setString(String.valueOf("Save OK"));
		Display.getDisplay(midlet).setCurrent(alert);
		return;
	}
	
	public Screen initCommand() {
		backCmd = new Command("返回上级", Command.CANCEL, 1);
		saveRuleCmd = new Command("保存修改", Command.ITEM, 1);
		screen.addCommand(backCmd);
		screen.addCommand(saveRuleCmd);
		return this;
	}

	public void setCommandListener(CommandListener cmdl) {
		screen.setCommandListener(cmdl);
	}
	
	public Screen initUI(MIDlet midlet) {
		screen = new Form("查看规则");
		ruleName = new TextField("name", "", 100, 0);
		ruleDescrib = new TextField("description", "", 100, 0);
		ruleContent = new TextField("content", "", 2000, 0);
		screen.append(ruleName);
		screen.append(ruleDescrib);
		screen.append(ruleContent);
		this.midlet = midlet;
		return this;
	}
	public void display(MIDlet midlet) {
		String name = SharePool.getInstance().currentRuleName;
		int id = -1;
		try {
			id = RuleManager.getInstance().lookupRule(name);
			if (id == -1) {
				Alert alert = new Alert("Error");
				alert.setType(AlertType.ERROR);
				alert.setString(String.valueOf("没有这个规则"));
				Display.getDisplay(midlet).setCurrent(alert);
				RuleViewerScreen.getInstance().display(midlet);
			}
			Rule rule = RuleManager.getInstance().getRule(id);
			ruleName.setString(rule.name);
			ruleDescrib.setString(rule.describ);
			ruleContent.setString(rule.content);
		} catch (VerifierException e) {
			e.printStackTrace();
			Alert alert = new Alert("Error");
			alert.setType(AlertType.ERROR);
			alert.setString(String.valueOf("打开规则失败"));
			Display.getDisplay(midlet).setCurrent(alert);
			RuleViewerScreen.getInstance().display(midlet);
		}
		
		
		Display.getDisplay(midlet).setCurrent(screen);
	}
}
